package com.cmc.cloud.cmclink.doc.service.impl.si;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import com.cmc.cloud.cmclink.doc.api.document.emums.CalculateTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.CalculateOperateLogErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.CalculateOperateLogConvert;
import com.cmc.cloud.cmclink.doc.entity.CalculateOperateLogDO;
import com.cmc.cloud.cmclink.doc.mapper.CalculateOperateLogMapper;
import com.cmc.cloud.cmclink.doc.service.si.CalculateOperateLogService;
import com.cmc.cloud.cmclink.doc.vo.calculatelogvo.CalculateOperateLogCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.calculatelogvo.CalculateOperateLogUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 计费日志 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class CalculateOperateLogServiceImpl implements CalculateOperateLogService {

    @Resource
    private CalculateOperateLogMapper calculateOperateLogMapper;

    @Override
    public void createCalculateOperateLog(CalculateOperateLogCreateReqVO createReqVO) {
        // 插入
        CalculateOperateLogDO calculateOperateLog = CalculateOperateLogConvert.INSTANCE.convert(createReqVO);
        calculateOperateLogMapper.insert(calculateOperateLog);
    }

    @Override
    public void updateCalculateOperateLog(CalculateOperateLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateCalculateOperateLogExists(updateReqVO.getId());
        // 更新
        CalculateOperateLogDO updateObj = CalculateOperateLogConvert.INSTANCE.convert(updateReqVO);
        calculateOperateLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteCalculateOperateLog(Long id) {
        // 校验存在
        validateCalculateOperateLogExists(id);
        // 删除
        calculateOperateLogMapper.deleteById(id);
    }

    private void validateCalculateOperateLogExists(Long id) {
        if (calculateOperateLogMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(CalculateOperateLogErrorCodeConstants.CAL_OPERATE_NOT_EXIST);
        }
    }

    @Override
    public CalculateOperateLogDO getCalculateOperateLog(Long id) {
        return calculateOperateLogMapper.selectById(id);
    }

    @Override
    public List<CalculateOperateLogDO> getCalculateOperateLogList(Collection<Long> ids) {
        return calculateOperateLogMapper.selectBatchIds(ids);
    }

    @Override
    public CalculateOperateLogDO getNewOperateLogByBlnoId(Long blnoId) {
        LambdaQueryWrapperX<CalculateOperateLogDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(CalculateOperateLogDO::getBusinessId, blnoId).orderByDesc(CalculateOperateLogDO::getId)
            .last("LIMIT 1");
        return calculateOperateLogMapper.selectOne(queryWrapperX);
    }

    @Override
    public CalculateOperateLogDO getNewOperateLogByBlnoId(Long blnoId, CalculateTypeEnum calculateType) {
        LambdaQueryWrapperX<CalculateOperateLogDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX
                .eq(CalculateOperateLogDO::getBusinessId, blnoId)
                .eq(CalculateOperateLogDO::getCalculateType, calculateType.getValue())
                .orderByDesc(CalculateOperateLogDO::getId)
            .last("LIMIT 1");
        return calculateOperateLogMapper.selectOne(queryWrapperX);
    }

}
